﻿namespace Utilities.Validation
{
    public static class DefaultValidatorExtensions
    {
        public static void SetRules()
        {

        }
        #region 为空判断

        /// <summary>
        /// 字段不能为空
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="message">提示信息</param>
        /// <returns></returns>
        public static DataValidate NotEmpty(this DataValidate opts,string message=null)
        {
            opts.SetOpts("required", true, message);
            return opts;
        }

        #endregion
        
        /// <summary>
        /// 必须为Email地址
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="message">提示信息</param>
        /// <returns></returns>
        public static DataValidate Email(this DataValidate opts,string message=null)
        {
            opts.SetOpts("email", true, message);
            return opts;
        }
        /// <summary>
        /// 必须为网址
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate Url(this DataValidate opts, string message = null)
        {
            opts.SetOpts("url", true, message);
            return opts;
        }
        /// <summary>
        /// 必须为相同值
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="idName">要比较的字段Id名称，不加#</param>
        /// <returns></returns>
        public static DataValidate EqualTo(this DataValidate opts, string idName, string message = null)
        {
            opts.SetOpts("equalTo", "#" + idName, message);
            return opts;
        }

        /// <summary>
        /// 正则验证
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="regex">正则表达式</param>
        /// <returns></returns>
        public static DataValidate Regex(this DataValidate opts, string regex, string message = null)
        {
            opts.SetOpts("regex", regex, message);       
            return opts;
        }
        /// <summary>
        /// 中文
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate AllChinese(this DataValidate opts, string message = null)
        {
            opts.SetOpts("allChinese", true, message);
            return opts;
        }
        /// <summary>
        /// 必须为不同值
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="idName">要比较的字段Id名称，不加#</param>
        /// <returns></returns>
        public static DataValidate NotEqualTo(this DataValidate opts, string idName, string message = null)
        {
            opts.SetOpts("notequalTo", "#" + idName, message);            
            return opts;
        }

        /// <summary>
        /// 最小值
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="min">最小值</param>
        /// <returns></returns>
        public static DataValidate Min(this DataValidate opts, double min, string message = null)
        {
            opts.SetOpts("min", min, message);        
            return opts;
        }

        /// <summary>
        /// 最大值
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="max">最大值</param>
        /// <returns></returns>
        public static DataValidate Max(this DataValidate opts, double max, string message = null)
        {
            opts.SetOpts("max", max, message);
            return opts;
        }

        /// <summary>
        /// 大小在指定区间
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns></returns>
        public static DataValidate Range(this DataValidate opts, double min, double max, string message = null)
        {
            var range = new[] { min, max };
            opts.SetOpts("range", range, message);     
            return opts;
        }

        /// <summary>
        /// 最小长度
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="minlength"></param>
        /// <returns></returns>
        public static DataValidate MinLength(this DataValidate opts, int minlength, string message = null)
        {
            opts.SetOpts("minlength", minlength, message);        
            return opts;
        }
        /// <summary>
        /// 最大长度
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="maxlength"></param>
        /// <returns></returns>
        public static DataValidate MaxLength(this DataValidate opts, int maxlength, string message = null)
        {
            opts.SetOpts("maxlength", maxlength,message);
            return opts;
        }

        /// <summary>
        /// 长度在指定区间
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="minlength">最小长度</param>
        /// <param name="maxlength">最大长度</param>
        /// <returns></returns>
        public static DataValidate RangeLength(this DataValidate opts, int minlength, int maxlength, string message = null)
        {
            var rangelength = new[] { minlength, maxlength };
            opts.SetOpts("rangelength", rangelength,message);
            return opts;
        }
        /// <summary>
        /// 最小长度(按字节)
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="minlength"></param>
        /// <returns></returns>
        public static DataValidate CharMinLength(this DataValidate opts, int minlength, string message = null)
        {
            opts.SetOpts("charMinlength", minlength,message);
            return opts;
        }
        /// <summary>
        /// 最大长度(按字节)
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="maxlength"></param>
        /// <returns></returns>
        public static DataValidate CharMaxLength(this DataValidate opts, int maxlength, string message = null)
        {
            opts.SetOpts("charMaxlength", maxlength,message);
            return opts;
        }

        /// <summary>
        /// 长度在指定区间(按字节)
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="minlength">最小长度</param>
        /// <param name="maxlength">最大长度</param>
        /// <returns></returns>
        public static DataValidate CharLength(this DataValidate opts, int minlength, int maxlength, string message = null)
        {
            var rangelength = new[] { minlength, maxlength };
            opts.SetOpts("charRangeLength", rangelength,message);
            return opts;
        }

        /// <summary>
        /// 身份证号
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate IsIdCardNo(this DataValidate opts, string message = null)
        {
            opts.SetOpts("isIdCardNo", true,message);
            return opts;
        }
        ///// <summary>
        ///// Ajax验证，提交的值参数为data，post提交-------------为测试估计有问题，function翻译这块
        ///// </summary>
        ///// <param name="opts"></param>
        ///// <param name="url">验证地址</param>
        ///// <returns></returns>
        //public static DataValidate Remote(this DataValidate opts,string url)
        //{
        //    var ajax = new {  url, type = "post", data = string.Format("function () {{return $(\"#{0}\").val();}}",opts.Name) };
        //    opts.SetRules("remote", ajax);
        //    return opts;
        //}
        /// <summary>
        /// 日期验证2013-04-10
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate DateTimeOrDate(this DataValidate opts, string message = null)
        {
            opts.SetOpts("dateTimeOrDate", true, message);
            return opts;
        }
        /// <summary>
        /// 日期时间验证2013-04-10 12：22：22
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate DateTime(this DataValidate opts, string message = null)
        {
            opts.SetOpts("dateTime", true,message);
            return opts;
        }
        /// <summary>
        /// 必须为整数
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate Integer(this DataValidate opts, string message = null)
        {
            opts.SetOpts("digits", true, message);
            return opts;
        }
        /// <summary>
        /// 必须为数字，可小数
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate Number(this DataValidate opts, string message = null)
        {
            opts.SetOpts("number", true, message);
            return opts;
        }
        /// <summary>
        /// 必须为ip地址
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate IsIp(this DataValidate opts, string message = null)
        {
            opts.SetOpts("isIP", true, message);
            return opts;
        }
        /// <summary>
        /// 不能包含Html
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate NoHtml(this DataValidate opts, string message = null)
        {
            opts.SetOpts("noHtml", true, message);
            return opts;
        }
        /// <summary>
        /// 不能包含中文或全角字符
        /// </summary>
        /// <param name="opts"></param>
        /// <returns></returns>
        public static DataValidate NoChinese(this DataValidate opts, string message = null)
        {
            opts.SetOpts("noChinese", true, message);
            return opts;
        }

        //public static DataValidate IsLegalXmlString(this DataValidate opts)
        //{
        //    opts.SetRules("IsLegalXmlString", true);
        //    return opts;
        //}
        /// <summary>
        /// 非 正则表达式，即不匹配此正则
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="regex">正则表达式</param>
        /// <returns></returns>
        public static DataValidate NotRegex(this DataValidate opts, string regex, string message = null)
        {
            opts.SetOpts("notRegex", regex, message);
            return opts;
        }

        /// <summary>
        /// Ajax远程验证
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="param">参数：new {url=url}（必须有Url）若有js动态参数则加j#前后</param>
        /// <returns></returns>
        public static DataValidate MyRemote(this DataValidate opts, object param, string message = null)
        {
            opts.SetOpts("myRemote", param, message);
            return opts;
        }

        /// <summary>
        /// Ajax远程验证
        /// </summary>
        /// <param name="opts"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static DataValidate MyRemote(this DataValidate opts, string url, string message = null)
        {
            var param = new {url=url};
            return opts.MyRemote(param, message);
        }
        //public static DataValidate CharCodeLength(this DataValidate opts)
        //{
        //    opts.SetRules("email", true);
        //    return opts;
        //}
    }
}
